package as3.component.manager
{
//	import com.gameGY.utils.ToolTipObject;
//	import com.gameGY.utils.ToolTipParse;
	import as3.component.ui.Element;
	import as3.component.ui.ElementContinerType;
	import as3.component.ui.ToolTipsBorder;
	import as3.extra.FaceValues;
	
	import com.seaengine.constData.Enums;
	import com.seaengine.proxy.ItemProxy;
	import com.seaengine.valueobject.BaseItemVo;
	import com.seaengine.valueobject.BaseVo;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Dictionary;
	
	
	
	public class ToolTipsManager
	{
		
		private static var instance:ToolTipsManager;
		
		private var _border1:ToolTipsBorder;
		
		private var _root:DisplayObjectContainer;
		
		private var _toolTipBox:Sprite;
		
		private var _textFiled:TextField;
		
		private var _border:ToolTipsBorder;
		
		private var _dictionary:Dictionary;
		
		private var _currentTarget:DisplayObject;
		
		private var _mouseOffX:Number = 12;
		private var _mouseOffY:Number = 20;
		
		/**装备比较临时记录*/
		private var tempVO:BaseItemVo; 
		
		//private var target:ToolTipObject;
		
		private var textFormat:TextFormat = new TextFormat(null, 12, 0xffffff);
		
		
		public function ToolTipsManager()
		{
			if (instance) return;
			
			init();
			
		}
		
		private function init():void
		{
			_toolTipBox = new Sprite;
			_textFiled = new TextField();
			_textFiled.selectable = false;
			_textFiled.wordWrap = true;
			_textFiled.multiline = true;
			textFormat.leading = 2;
			_textFiled.defaultTextFormat = textFormat;
			_textFiled.x = 5;
			_textFiled.y = 5;
//			_textFiled.filters = [new GlowFilter(0, 1, 2, 2, 3, 1)];
			_textFiled.cacheAsBitmap = true;
			_dictionary = new Dictionary(true);
			
			_border = new ToolTipsBorder();
			_border.cacheAsBitmap = true;
			
			_border1 = new ToolTipsBorder();
			
			_toolTipBox.addChildAt(_border, 0);
			_toolTipBox.cacheAsBitmap = true;
			_toolTipBox.mouseEnabled = false;
			//_toolTipBox.mouseChildren = false;
		}
		
		public static function getInstance():ToolTipsManager
		{
			if (!instance) {
				instance = new ToolTipsManager;
			}
			
			return instance; 
		}
		
		/**
		 * 
		 * @param root
		 * 
		 */		
		public function runAt(root:Stage):void
		{
			_root = root;
		}
		
		public function register(target:DisplayObject, info:*, tooltipsFromat:Class = null):void
		{
			if (target == null) 
			{
				return;
			}
			if (info is String) 
			{
				if (info == "") 
				{
					return;
				}
				_dictionary[target] = info;
				if (target == _currentTarget) 
				{
					nowUpdateData(info);
				}
			} 
			else if (info is Object) 
			{
				_dictionary[target] = info;
			} 
			target.addEventListener(MouseEvent.MOUSE_OVER, onOverHandler);
			target.addEventListener(MouseEvent.MOUSE_OUT, onOutHandler);
		}
		
		public function unRegister(target:DisplayObject):void
		{
			if (!target) return;
			close();
			if (_dictionary[target]) {
				target.removeEventListener(MouseEvent.MOUSE_OVER, onOverHandler);
				target.removeEventListener(MouseEvent.MOUSE_OUT, onOutHandler);
				_dictionary[target] = null;
				delete _dictionary[target];
			}
		}
		
		/**
		 * 装备比对
		 * @param id 装备ID
		 */		
		public function equipComparison(id:int, vo:BaseVo):void
		{
//			var xml:XML = ItemProxy.getInstance().equipmentDic[id];
//			if(xml != null)
//			{
//				target = ToolTipParse.toolTipsParse(xml, vo, true) as ToolTipObject;
//				_border1.x = _toolTipBox.x + _toolTipBox.width + 2;
//				_border1.y = _toolTipBox.y;
//				_border1.width = target.width + 18;
//				_border1.height = target.height + 10;
//				target.x = 7;
//				target.y = 7;
//				_border1.addChild(target);
//				_root.addChild(_border1);
//			}
		}
		
		/**
		 * 取消装备比对
		 * @param target
		 */		
		public function cancelEquipComparison():void
		{
//			if(target != null)
//			{
//				if(target.parent != null)
//				{
//					target.parent.removeChild(target);
//					target.destroy();
//					target = null;		
//				}
//			}
//			if(_border1 != null)
//			{
//				if(_border1.parent != null)
//				{
//					_border1.parent.removeChild(_border1);
//				}
//			}
//			tempVO = null;
		}
		
		public function close():void
		{
			if (_currentTarget)
			{
				_currentTarget.removeEventListener(MouseEvent.MOUSE_MOVE, onMoveHandler);
				if (_root&& _root.contains(_toolTipBox))
				{
					_root.removeChild(_toolTipBox);
				}
				_currentTarget = null;
			}
		}
//		private function commandClickHand(evt:Event):void
//		{
//			removeEventListener(MouseEvent.MOUSE_DOWN, commandClickHand);
//			if (_root && _root.contains(_toolTipBox))
//			{
//				_root.removeChild(_toolTipBox);
//			}
//			clearBox();
//			isShowingTips=false;
//			isShowingCommand = false;
//		}
//		private var isShowingCommand:Boolean = false;
//		public function showCommandList(info:*):void
//		{
//			var dis:DisplayObject = info as DisplayObject;
//			_toolTipBox.addChild(dis);
//			reSizeBg(dis.width, dis.height);
//			_root.addChild(_toolTipBox);
//			_root.addEventListener(MouseEvent.MOUSE_DOWN,commandClickHand);
//			_toolTipBox.x = _root.mouseX + _mouseOffX;
//			_toolTipBox.y = _root.mouseY + _mouseOffY;
//			isShowingTips = true;
//			isShowingCommand = true;
//			showTipsOldX = _root.mouseX;
//			showTipsOldY = _root.mouseY;
//			updateToolTipsPos(showTipsOldX, showTipsOldY)
//		}
		private var isShowingTips:Boolean=false;
		private var showTipsOldX:Number;
		private var showTipsOldY:Number;
		public function showTips(info:*):void
		{
			if (_root)
			{
//				var temp:DisplayObject = evt.currentTarget as DisplayObject;
				clearBox();
//				var info:* = _dictionary[temp];
//				_currentTarget = temp;
//				_currentTarget.addEventListener(MouseEvent.MOUSE_MOVE, onMoveHandler);
				_toolTipBox.x = _root.mouseX + _mouseOffX;
				_toolTipBox.y = _root.mouseY + _mouseOffY;

				if (info is String)
				{
					_textFiled.width = 150;
					_textFiled.height = 20;
					_textFiled.text = "";
					_textFiled.htmlText = "";
					_textFiled.defaultTextFormat = textFormat;
					_textFiled.htmlText = info;
					_textFiled.width = _textFiled.textWidth + 8;
					if (_textFiled.width > 180)
					{
						_textFiled.width = 180;
					}
					_textFiled.height = _textFiled.textHeight + 4;
					_textFiled.mouseEnabled=false;
					_textFiled.selectable=false;
					_toolTipBox.addChild(_textFiled);
					reSizeBg(_textFiled.width, _textFiled.height);
					_root.addChild(_toolTipBox);
					_root.addEventListener(MouseEvent.MOUSE_MOVE, onMoveHandler);

					_toolTipBox.x = _root.mouseX + _mouseOffX;
					_toolTipBox.y = _root.mouseY + _mouseOffY;
					isShowingTips = true;
					showTipsOldX = _root.mouseX;
					showTipsOldY = _root.mouseY;
					updateToolTipsPos(showTipsOldX, showTipsOldY)
					//onMoveHandler(new MouseEvent(MouseEvent.MOUSE_MOVE,true,false,_root.mouseX,_root.mouseY))
				}
				else if (info is Object)
				{
					var dis:DisplayObject = info as DisplayObject;
					dis.x = 7;
					dis.y = 7;
					_toolTipBox.addChild(dis);
					reSizeBg(dis.width, dis.height);
					_root.addChild(_toolTipBox);
					_root.addEventListener(MouseEvent.MOUSE_MOVE, onMoveHandler);
					
					_toolTipBox.x = _root.mouseX + _mouseOffX;
					_toolTipBox.y = _root.mouseY + _mouseOffY;
					isShowingTips = true;
					showTipsOldX = _root.mouseX;
					showTipsOldY = _root.mouseY;
					updateToolTipsPos(showTipsOldX, showTipsOldY)
				}
			}
		}
		protected function onOverHandler(evt:MouseEvent):void
		{
			
			if (_root)
			{
				var temp:DisplayObject = evt.currentTarget as DisplayObject;
				clearBox();
				var info:* = _dictionary[temp];
				_currentTarget = temp;
				_currentTarget.addEventListener(MouseEvent.MOUSE_MOVE, onMoveHandler);
				_toolTipBox.x = evt.stageX + _mouseOffX;
				_toolTipBox.y = evt.stageY + _mouseOffY;
				
				if (info is String)
				{
					_textFiled.width = 150;
					_textFiled.height = 20;
					_textFiled.text = "";
					_textFiled.htmlText = "";
					_textFiled.defaultTextFormat = textFormat;
					_textFiled.htmlText = info;
					_textFiled.width = _textFiled.textWidth + 8;
					_textFiled.height = _textFiled.textHeight + 4;
					_toolTipBox.addChild(_textFiled);
					reSizeBg(_textFiled.width, _textFiled.height);
					_root.addChild(_toolTipBox);
				} 
				else if (info is Object)
				{
					var dis:DisplayObject = info as DisplayObject;
					if(!dis)
						return;
					dis.x = 7;
					dis.y = 7;
					_toolTipBox.addChild(dis);
					reSizeBg(dis.width, dis.height);
					_root.addChild(_toolTipBox);
					if(temp is Element && ElementContinerType.CURRENT_TYPE == ElementContinerType.TYPE_BACKPACK_EQUIPMENT)
					{
						var element:Element = temp as Element;
						if(element.vo != null)
						{
							var d:DisplayObject = evt.target as DisplayObject;
							while(d)
							{
								//if(d is BackPackPanel)
								//{
									if(element.vo.type == Enums.ITEM_TYPE_SPECIAL)//奇物
									{
										tempVO = ItemProxy.getInstance().getEquipmentVO(8);
										if(tempVO == null)
										{
											tempVO = ItemProxy.getInstance().getEquipmentVO(9);
										}
										if(tempVO == null)
										{
											tempVO = ItemProxy.getInstance().getEquipmentVO(10);
										}
										if(tempVO == null)
										{
											tempVO = ItemProxy.getInstance().getEquipmentVO(11);
										}
									}
									else
									{
										tempVO = ItemProxy.getInstance().getEquipmentVO(equipIndex(element.vo.type));
									}
									if(tempVO != null)
									{
										equipComparison(tempVO.itemId, tempVO);
									}
									break;
								//}
								//else
								//{
								//	d = d.parent;
								//}
							}
						}
					}
				} 
			}
		}
		
		private function nowUpdateData(info:Object):void
		{
			_textFiled.width = 180;
			_textFiled.height = 20;
			_textFiled.text = "";
			_textFiled.htmlText = "";
			_textFiled.defaultTextFormat = textFormat;
			_textFiled.htmlText = info as String;
			_textFiled.width = _textFiled.textWidth + 8;
			if (_textFiled.width > 180) {
				_textFiled.width = 180;
			}
			_textFiled.height = _textFiled.textHeight + 4;
			reSizeBg(_textFiled.width, _textFiled.height);
		}
		
		protected function onOutHandler(evt:MouseEvent):void
		{
			//if(target != null)
			//{
				cancelEquipComparison();
			//}
//			if(isShowingCommand)
//				return;
			if (_currentTarget)
			{
				_currentTarget.removeEventListener(MouseEvent.MOUSE_MOVE, onMoveHandler);
				if (_root && _root.contains(_toolTipBox))
				{
					_root.removeChild(_toolTipBox);
				}
			}
			clearBox();
		}
		private function updateToolTipsPos(mouseX:Number,mouseY:Number):void
		{
			//if(target != null)//有装备对比
			//{
			//	_toolTipBox.x = (mouseX+ _mouseOffX + _border1.width + _border.width) > (FaceValues.FACE_X + FaceValues.FACE_OFFSET_X) ? (mouseX - _mouseOffX - _border.width) : (mouseX + _mouseOffX);
			//	_toolTipBox.y = (mouseY + _mouseOffY + _border1.height) > FaceValues.FACE_OFFSET_Y ? (mouseY - _border1.height < 0 ? 0 : mouseY - _border1.height) : (mouseY + _mouseOffY);
			//	
			//	_border1.x = (mouseX + _mouseOffX + _border1.width + _border.width) > (FaceValues.FACE_X + FaceValues.FACE_OFFSET_X) ? (mouseX - _mouseOffX - _border1.width - _border.width - 2) : (mouseX + _mouseOffX + _border.width + 2);
			//	_border1.y = (mouseY + _mouseOffY + _border1.height) > FaceValues.FACE_OFFSET_Y ? (mouseY - _border1.height < 0 ? 0 : mouseY - _border1.height) : (mouseY + _mouseOffY);
			//}
			//else//无装备对比
			//{
				/**sprite的宽高是包含隐藏的在可视范围外的最小矩形，所以用背景的宽高*/
				_toolTipBox.x = (mouseX+ _mouseOffX + _border.width) > (FaceValues.FACE_X + FaceValues.FACE_OFFSET_X) ? (mouseX - _border.width) : (mouseX + _mouseOffX);
				_toolTipBox.y = (mouseY + _mouseOffY + _border.height) > FaceValues.FACE_OFFSET_Y ? (mouseY - _border.height < 0 ? 0 : mouseY - _border.height) : (mouseY + _mouseOffY);
			//}
		}
		private function onMoveHandler(evt:MouseEvent):void
		{
			if (_currentTarget||isShowingTips)
			{
				if (_root)
				{
					updateToolTipsPos(evt.stageX,evt.stageY)
					evt.updateAfterEvent();
				}
			}
			if(isShowingTips)
			{
				if(Math.abs(evt.stageX-showTipsOldX)>15 || Math.abs(evt.stageY-showTipsOldY)>15)
				{
					_root.removeEventListener(MouseEvent.MOUSE_MOVE, onMoveHandler);
					if (_root && _root.contains(_toolTipBox))
					{
						_root.removeChild(_toolTipBox);
					}
					clearBox();
					isShowingTips=false;
				}
				
			}
		}
		
		private function reSizeBg(w:Number, h:Number):void
		{
			_border.width = w + 18;
			_border.height = h + 10;
		}
		
		private function clearBox():void
		{
			var len:int = _toolTipBox.numChildren;
			while(len > 0) 
			{
				_toolTipBox.removeChildAt(0);
				len--;
			}
			_toolTipBox.addChild(_border);
			_currentTarget = null;
		}
		
		public function currentTarget():DisplayObject
		{
			return _currentTarget;
		}

		/**
		 *  获得装备在人物面板下标
		 * @param type 装备类型
		 */		
		private function equipIndex(type:uint):int
		{
			var result:int;
			switch(type)
			{
				case Enums.ITEM_TYPE_SWORD:
				case Enums.ITEM_TYPE_SABRE:
				case Enums.ITEM_TYPE_DAGGER:
				case Enums.ITEM_TYPE_DOUBLE_WEAPON:
				case Enums.ITEM_TYPE_LONG_WEAPON:
				case Enums.ITEM_TYPE_GLOVE:
					result = 12;
					break;
				case Enums.ITEM_TYPE_HEADDRESS:
					result = 0;
					break;
				case Enums.ITEM_TYPE_JACKET:
					result = 1;
					break;
				case Enums.ITEM_TYPE_PANTS:
					result = 2;
					break;
				case Enums.ITEM_TYPE_SHOES:
					result = 3;
					break;
				case Enums.ITEM_TYPE_CHARM:
					result = 4;
					break;
				case Enums.ITEM_TYPE_CAPE:
					result = 5;
					break;
				case Enums.ITEM_TYPE_WRISTLET:
					result = 6;
					break;
				case Enums.ITEM_TYPE_SPECIAL:
					result = 8;
					break;
				case Enums.ITEM_TYPE_PENDANT:
					result = 7;
					break;
				default :
					result = -1;
					break;
			} 
			return result;
		}
		
	}
}